package com.ruoyi.business.service;

import cn.hutool.Hutool;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.business.domain.CivilServants;
import com.ruoyi.business.domain.vo.ResumeVo;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static java.awt.SystemColor.text;

@Service
public class ResumeService {

    @Value("${tempFolder}")
    private String tempFolder;

    /**
     * 个人简历生成Excel文件
     * @param resumeVo
     * @param fileTemplate
     * @return
     */
    public String createNewExcel(ResumeVo resumeVo, String fileTemplate,String randomName) throws IOException {

        File folder=new File(tempFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        CivilServants civilServants = resumeVo.getCivilServants();
        List<ResumeVo.ListSummary> listSummary = resumeVo.getListSummary();

        StringBuilder positiveList = new StringBuilder();
        StringBuilder negativeList = new StringBuilder();
        for (ResumeVo.ListSummary summary : listSummary) {
            if (summary.getProsCons().equals("0")){
                positiveList.append(summary.getWorkList()+"，");
            }else {
                negativeList.append(summary.getWorkList()+"，");
            }
        }

        // 处理positiveList，去除最后一个逗号并添加句号
        if (positiveList.length() > 0) {
            positiveList.setLength(positiveList.length() - 1); // 移除最后一个字符（逗号）
            positiveList.append("。");
        }

// 处理negativeList，去除最后一个逗号并添加句号
        if (negativeList.length() > 0) {
            negativeList.setLength(negativeList.length() - 1); // 移除最后一个字符（逗号）
            negativeList.append("。");
        }


        try {
            FileInputStream inputStream = new FileInputStream(fileTemplate);
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            //基本信息
            Row row3 = sheet.getRow(2);
            row3.getCell(1).setCellValue(civilServants.getName());
            Integer sex = civilServants.getSex();
            if (sex==0){
                row3.getCell(3).setCellValue("男");
            } else if (sex==1) {
                row3.getCell(3).setCellValue("女");
            }

            row3.getCell(7).setCellValue(civilServants.getBirthTime());

            Row row4 = sheet.getRow(3);
            row4.getCell(1).setCellValue(civilServants.getEthnicGroup());
            row4.getCell(3).setCellValue(civilServants.getNativePlace());
            row4.getCell(7).setCellValue(civilServants.getDepartment());

            Row row5 = sheet.getRow(4);
            row5.getCell(1).setCellValue(civilServants.getCommitteeTime());
            row5.getCell(3).setCellValue(civilServants.getNowLevelTime());
            row5.getCell(7).setCellValue(civilServants.getNowPostTime());

            Row row7 = sheet.getRow(6);
            row7.getCell(1).setCellValue(civilServants.getPartyTime());
            row7.getCell(3).setCellValue(civilServants.getDirectorTime());
            row7.getCell(7).setCellValue(civilServants.getNowOfficeTime());

            //学历学位信息
            Row row9 = sheet.getRow(8);
            row9.getCell(2).setCellValue(civilServants.getFullTimeDegree());
            row9.getCell(6).setCellValue(civilServants.getFullTimeGraduation());

            Row row11 = sheet.getRow(10);
            row11.getCell(2).setCellValue(civilServants.getOnJobDegree());
            row11.getCell(6).setCellValue(civilServants.getOnJobGraduation());

            //职务
            Row row13 = sheet.getRow(12);
            row13.getCell(2).setCellValue(civilServants.getPosition());

            //正面清单
            Row row14 = sheet.getRow(13);
            row14.getCell(1).setCellValue(positiveList.toString());

            //负面清单
            Row row15 = sheet.getRow(14);
            row15.getCell(1).setCellValue(negativeList.toString());


            FileOutputStream outputStream = new FileOutputStream(folder+"/"+randomName+".xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+randomName+".xlsx");
        return file.getPath();
    }

    public boolean removeResume(String location) {
        int lastIndexOfDot = location.lastIndexOf('.');
        String pathWithoutExtension = lastIndexOfDot == -1 ? location : location.substring(0, lastIndexOfDot);
        String excelLocation=pathWithoutExtension+".xlsx";

        File pdfFile = new File(location);
        File excelFile = new File(excelLocation);

        boolean pdfDelete;
        boolean excelDelete;

        // 检查文件是否存在
        if (pdfFile.exists()) {
            pdfDelete = pdfFile.delete();
        }else {
            throw new RuntimeException("pdf缓存文件不存在");
        }

        // 检查文件是否存在
        if (excelFile.exists()) {
            excelDelete  = excelFile.delete();
        }else {
            throw new RuntimeException("excel缓存文件不存在");
        }

        if (pdfDelete&&excelDelete){
            return true;
        }
        return false;
    }
}
